package cn.wolfcode.p2p.business.service.impl;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.business.domain.AccountFlow;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

        @Autowired
        private AccountFlowMapper accountFlowMapper;
        @Override
        public void createBaseAccountFlow(BigDecimal amount,Account account,int actionType,String note){
                AccountFlow accountFlow = new AccountFlow();
                accountFlow.setAccountId(account.getId());
                accountFlow.setActionType(actionType);
                accountFlow.setAmount(amount);
                accountFlow.setNote(note);
                accountFlow.setUsableAmount(account.getUsableAmount());
                accountFlow.setFreezedAmount(account.getFreezedAmount());
                accountFlow.setActionTime(new Date());
                accountFlowMapper.insert(accountFlow);
        }

        @Override
        public void createRechargeOfflineAccountFlow(BigDecimal amount, Account account) {
              /*  AccountFlow accountFlow = new AccountFlow();
                accountFlow.setAccountId(account.getId());
                accountFlow.setActionType(Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
                accountFlow.setAmount(amount);
                accountFlow.setNote("线下充值,充值金额:"+amount);
                accountFlow.setUsableAmount(account.getUsableAmount());
                accountFlow.setFreezedAmount(account.getFreezedAmount());
                accountFlow.setActionTime(new Date());
                accountFlowMapper.insert(accountFlow);*/
                createBaseAccountFlow(amount,account,Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE,"线下充值,充值金额:"+amount);
        }

        @Override
        public void createBidFreezedFlow(BigDecimal amount, Account account) {
                createBaseAccountFlow(amount,account,Constants.ACCOUNT_ACTIONTYPE_BID_FREEZED,"参与投标,投标金额:"+amount);
        }

        @Override
        public void createBidFailureFlow(BigDecimal amount, Account account, BidRequest br) {
                createBaseAccountFlow(amount,account,Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED,"投标"+br.getTitle()+"失败," +
                        "冻结金额减少:"+amount+"可用金额增加:"+amount);
        }

        @Override
        public void createBorrowSuccessFlow(BigDecimal amount, Account account, BidRequest br) {
                createBaseAccountFlow(amount,account,Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL,"您的借款:"+br.getTitle()+"成功," +
                        "借款金额:"+amount);
        }

        @Override
        public void createBorrowAccountManagementChargeFlow(BigDecimal amount, Account account, BidRequest br) {
                createBaseAccountFlow(amount,account,Constants.ACCOUNT_ACTIONTYPE_CHARGE,"支付借款:["+br.getTitle()+"]的平台管理费:" +amount);
        }

        @Override
        public void createBidSuccessFlow(Account biderAccount, BigDecimal availableAmount, BidRequest br) {
                createBaseAccountFlow(availableAmount,biderAccount,Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL,"投标:["+br.getTitle()+"]成功,减少冻结金额:" +availableAmount);
        }

        @Override
        public void createReturnMoneyFlow(Account borrowerAccount, PaymentSchedule ps) {
                createBaseAccountFlow(ps.getTotalAmount(),borrowerAccount,Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL,"借款:["+ps.getBidRequestTitle()+"]第"+ps.getMonthIndex()
                        +"期还款成功,减少可用金额:" +ps.getTotalAmount());
        }

        @Override
        public void createCollectionFlow(Account account, BigDecimal totalAmount, PaymentSchedule ps) {
                createBaseAccountFlow(totalAmount,account,Constants.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY,"收到:["+ps.getBidRequestTitle()+"]第"+ps.getMonthIndex()
                        +"期还款,可用金额增加:" +totalAmount);
        }

        @Override
        public void createInterestManagerChargeFlow(Account account, BigDecimal interestManagerCharge) {
                createBaseAccountFlow(interestManagerCharge,account,Constants.ACCOUNT_ACTIONTYPE_INTEREST_SHARE,
                        "支付平台利息管理费,可用金额减少:" +interestManagerCharge);
        }

        @Override
        public void createCreditTransferSuccessFlow(Account transferFromAccount, BigDecimal bidRequestAmount) {
                createBaseAccountFlow(bidRequestAmount,transferFromAccount,Constants.ACCOUNT_ACTIONTYPE_TRANSFER_SUCCESS,
                        "转让债权标成功,可用余额增加:" +bidRequestAmount);
        }

        @Override
        public void createCreditTransferForSubscribeFlow(Account currentAccount, BigDecimal bidRequestAmount) {
                createBaseAccountFlow(bidRequestAmount,currentAccount,Constants.ACCOUNT_ACTIONTYPE_SUBSCRIBE_SUCCESS,
                        "认购债权标成功,可用余额减少:" +bidRequestAmount);
        }

        @Override
        public void createRechargeOnline(Account account, BigDecimal amount) {
                createBaseAccountFlow(amount,account,Constants.ACCOUNT_ACTIONTYPE_RECHARGE_ONLINE,"线上充值成功,充值金额:"+amount);
        }


}
